package demo.java.java8.Lambdas.demo5;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.function.IntSupplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import javax.swing.plaf.synth.SynthSpinnerUI;

public class BuildStream {

	public static void main(String[] args) {
//		//由值创建流
//		Stream<String> stream=Stream.of("Java 8", "Lambdas", "In", "Action");
//		stream.map(String::toUpperCase).forEach(System.out::println);
//		
//		//得到空流
//		Stream<String> emptyStream=Stream.empty();
//		
//		//数组创建流
//		int[] numbers = {2,3,5,7,11,13};
//		int sum=Arrays.stream(numbers).sum();
//		System.out.println(sum);
//		
//		//文件生成流
//		long uniqueWords=0;
//		Stream<String> lines;
//		try {
//			lines = Files.lines(Paths.get("E:\\result.txt"),Charset.defaultCharset());
//			List<String> collect = lines.flatMap(line ->Arrays.stream(line.split(" ")))
//					.distinct()
//					.collect(Collectors.toList());
//			System.out.println(collect);
//			lines = Files.lines(Paths.get("E:\\result.txt"),Charset.defaultCharset());
//			uniqueWords = lines.flatMap(line ->Arrays.stream(line.split(" ")))
//					.distinct()
//					.count();
//			System.out.println(uniqueWords);
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
//		//函数生成流：创建无限流
//		Stream.iterate(0, n -> n+2).limit(10).forEach(System.out::println);
//		
//		Stream.iterate(new int[]{0,1}, t -> new int[]{t[1],t[0]+t[1]})
//		.limit(20)
//		.forEach(t -> System.out.println("("+t[0]+","+t[1]+")"));
//		Stream.iterate(new int[]{0,1}, t -> new int[]{t[1],t[0]+t[1]})
//		.limit(20).map(t ->  t[0])
//		.forEach(System.out::println);
//		
//		Stream.generate(Math::random).limit(5).forEach(System.out::println);
		
		IntSupplier fib=new IntSupplier() {
			private int previous=0;
			private int current=1;
			@Override
			public int getAsInt() {
				int oldPrevious=this.previous;
				int nextValue=this.previous+this.current;
				this.previous=this.current;
				this.current=nextValue;
				return oldPrevious;
			}
		};
		IntStream.generate(fib).limit(10).forEach(System.out::println);
	}

}
